package com.dfrz.household.controller;

import com.dfrz.household.bean.*;
import com.dfrz.household.service.*;
import com.dfrz.household.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
public class OrderController {
    @Autowired
    IShopCartService shopCartService;
    @Autowired
    IOrderService orderService;
    @Autowired
    IAddressService addressService;
    @Autowired
    IOrder_detailsService order_detailsService;
    @RequestMapping("/suborder")
    public Result generateOrder(int []list,Address address,  HttpSession session,Integer totalprice,String time){
        User user= (User) session.getAttribute("login_user");
        Order order = new Order();
        Order_details order_details = new Order_details();
        Result result = new Result();
        if (list.length > 0){
            address.setUserid(user.getUid());
            addressService.insertAddress(address);
            order.setOno(String.valueOf(new Date().getTime()));
            order.setUid(user.getUid());
            order.setUname(user.getUname());
            order.setTotalprice(totalprice);
            order.setTime(time);
            Integer count=orderService.insertOrder(order);
            for (int i : list) {
                ShopCart cart=shopCartService.getCartById(i);
                order_details.setOid(count);
                order_details.setCount(cart.getCount());
                order_details.setServerid(cart.getSdid());
                order_details.setTotalprice(cart.getTotalprice());
                int flag=order_detailsService.insertOrder_details(order_details);
                if (flag>0){
                    int code=shopCartService.deleteShopCart(i);
                }


            }

            result.setCode(0);
        }
        return  result;
    }
}
